﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace WebSOAPUI.Lib
{
    public class XmlUtils
    {
        public static string FormatXml(string xml)
        {
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            var retXml = xmlDoc.OuterXml;
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            XmlWriter xtw = null;
            try
            {
                var setting = new XmlWriterSettings();
                setting.ConformanceLevel = ConformanceLevel.Auto;
                //setting.NewLineChars = "\r\n";
                setting.Indent = true;
                setting.IndentChars = "\t";
                setting.NewLineHandling = NewLineHandling.Entitize;
                xtw = XmlWriter.Create(sb, setting);
                //xtw.Formatting = Formatting.Indented;
                //xtw.Indentation = 1;
                
                //xtw.IndentChar = '\t';
                xmlDoc.WriteTo(xtw);
            } finally
            {
                if (xtw != null)
                    xtw.Close();
            }
            return sb.ToString();
        }
    }
}